from flask import Blueprint, current_app, request, jsonify

dbsql = Blueprint('dbsql', __name__)


@dbsql.route('/dbsql', methods=['POST'])
def login():
    with current_app.app_context():
        data = request.get_json()
        username = data.get('username')
        password = data.get('password')

        pool = current_app.config['DB_POOL']
        with pool.connection() as conn:
            with conn.cursor() as cursor:
                sql = "SELECT * FROM sys_user WHERE username = %s AND password = %s"
                cursor.execute(sql, (username, password))
                result = cursor.fetchone()

        if result:
            logger = current_app.config['custom_logger'].get_logger()
            logger.info('登录成功 ' + username)
            return jsonify({'message': 'Login successful'})
        else:
            logger = current_app.config['custom_logger'].get_logger()
            logger.error('登录失败 ' + username)
            return jsonify({'message': 'Login failed'})
